package com.fw.dao.impl;

import java.util.List;

import com.fw.dao.EcommerceDao;
import com.fw.model.Ecommerce;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

public class EcommerceDaoImpl implements EcommerceDao {

	@Override
	public boolean add_ecommerceDao(Ecommerce ecommerce) {
		return ecommerce.save();
	}

	@Override
	public boolean delete_ecommerceDao(Ecommerce ecommerce) {
		String sql = "delete from e_commerce where id = ?";
		int i = Db.update(sql, ecommerce.getInt("id"));
		if(i > 0) return true;
		return false;
	}

	@Override
	public Ecommerce query_ecommerceByIdDao(Ecommerce ecommerce) {
		String sql = "select * from e_commerce where id = ?";
		List<Ecommerce> list = ecommerce.find(sql, ecommerce.getInt("id"));
		if(list.size() > 0) return list.get(0);
		return null; 
	}

	@Override
	public List<Ecommerce> query_allEcommerceDao() {
		String sql = "select * from e_commerce";
		List<Ecommerce> list = Ecommerce.ecommerceDao.find(sql);
		return list;
	}   
	@Override
	public int query_totalEcommerceByPageDao(String sql, List<Object> par) {
		String s = "select count(*) from e_commerce e where 1 = 1 "+sql;
		long count = Db.queryLong(s, par.toArray());
		return (int) count;
	}

	@Override
	public List<Ecommerce> query_allEcommerceByPageDao(String sql,
			int firstPage, int maxPage, List<Object> par) {
		String s = "select * from e_commerce e where 1 = 1 "+ sql + " limit ?,?";
		par.add(firstPage);
		par.add(maxPage);
		List<Ecommerce> list = Ecommerce.ecommerceDao.find(s, par.toArray());
		return list;
	}

	@Override
	public boolean update_ecommerceByIdDao(Ecommerce ecommerce) {
		Record r = Db.findById("e_commerce", "id",ecommerce.getInt("id"))
				.set("e_commerce", ecommerce.getStr("e_commerce"));
		return Db.update("e_commerce","id", r);
	}
}
